<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    ></SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['danger:create']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="220"
    >
      <template #actionButton="{ row }">
        <el-button
          type="text"
          v-hasPermi="['danger:location']"
          @click="openMap(row)"
          >查看位置</el-button
        >
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['danger:update']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['danger:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['addressInfo']"
    >
      <template #addressInfo>
        <el-form-item label="区域绘制" prop="addressInfo">
          <el-button type="primary" @click="openMap(null)">位置维护</el-button>
        </el-form-item>
      </template>
    </DialogVform>
    <MapDialog
      ref="map"
      @change="mapChange"
      :useButton="useButton"
      @close="closeMap"
      mapId="zdwxy"
    ></MapDialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { qingdan as APIS } from "@/api/hdanger/qingdan/index.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import MapDialog from "@/components/mapDialog/index";
import { nextTick } from "vue";
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) => ["dangerName", "dangerLevel"].includes(i.prop)),
});
const showSearch = ref(true);

const dialogVform = ref();

const table = ref();
const plugEdit = (row) => {
  if (row.addressInfo) {
    points.value = JSON.parse(row.addressInfo);
  } else {
    points.value = [];
  }
};
const plugAdd = () => {
  points.value = [];
};
const plugSubmit = (data) => {
  return {
    ...data,
    addressInfo: JSON.stringify(points.value),
  };
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
  plugAdd,
  plugSubmit,
});
const showMap = ref(false);
const map = ref();
const points = ref([]);
const useButton = ref(true);
const openMap = (row) => {
  showMap.value = true;
  nextTick().then(() => {
    if (row && row.addressInfo) {
      useButton.value = false;
      map.value.open(JSON.parse(row.addressInfo));
      return;
    }
    if (row) {
      useButton.value = false;
      map.value.open([]);
    } else {
      useButton.value = true;
      map.value.open(points.value);
    }
  });
};
const closeMap = () => {
  showMap.value = false;
};
const mapChange = (data) => {
  points.value = data;
};
</script>

<style lang="scss" scoped></style>
